package com.hyl.icon;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class DynamicTemplateClass {

    public BufferedImage generateBrandImage(MultipartFile backgroundFile, MultipartFile logoFile, String brandName, String phone, String address) {
        //TODO根据logo名的多少去数据库查询对应的模板
        try {
            // 目标图像尺寸
            int width = 992;
            int height = 3622;
            // 读取背景图
            BufferedImage background = ImageIO.read(backgroundFile.getInputStream());
            BufferedImage logo = ImageIO.read(logoFile.getInputStream());
            // 创建目标图像
            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = result.createGraphics();

            // 开启抗锯齿
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // 绘制背景
            g.drawImage(background, 0, 0, width, height, null);

            // 计算安全区域 (避免占用背景的底部 1/3)
            int safeHeight = (int) (height * 2.0 / 3);
            int margin = 150; // 增加 logo 到顶部的距离

            // 计算 Logo 位置 (缩小至背景宽度的 2/3 和背景高度的 1/5)
            int logoMaxWidth = width / 2 ;  // 将 logo 宽度设置为背景宽度的 1/2
            int logoMaxHeight = safeHeight / 5;
            int logoWidth = Math.min(logoMaxWidth, logo.getWidth());
            int logoHeight = Math.min(logoMaxHeight, logo.getHeight());
            int logoX = (width - logoWidth) / 2;
            int logoY = margin;
            g.drawImage(logo, logoX, logoY, logoWidth, logoHeight, null);

            // 设定字体
            // 设定品牌名称字体
            g.setFont(new Font("SansSerif", Font.BOLD, 400));
            g.setColor(Color.BLACK);
            FontMetrics brandMetrics = g.getFontMetrics();

            // 计算品牌名称整体高度
            int fontSize = 400;
            int textX = (width - fontSize) / 2;

            // 计算品牌名称起始 Y 位置，确保整体居中
            int brandY = logoY +  logoHeight + fontSize;
            // 竖排绘制品牌名称
            for (char c : brandName.toCharArray()) {
                g.drawString(String.valueOf(c), textX, brandY);
                brandY += fontSize;  // 每个字的间距等于字体大小
            }

            // 手机电话
            g.setFont(new Font("SansSerif", Font.BOLD, 50));

            int phoneY = 1982;
            System.out.println("phoneY"+phoneY);
            drawCenteredString(g, phone, width, phoneY);

            // 地址
            int addressY = phoneY + 100;
            drawCenteredString(g, address, width, addressY);

            g.dispose();
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 居中绘制字符串
    private void drawCenteredString(Graphics2D g, String text, int width, int y) {
        FontMetrics metrics = g.getFontMetrics();
        int x = (width - metrics.stringWidth(text)) / 2;
        g.drawString(text, x, y);
    }
}
